package org.hank.demoproject.iso8583_1987

import android.app.Activity
import android.os.Bundle
import android.view.LayoutInflater
import com.google.gson.Gson
import com.solab.iso8583.IsoMessage
import com.solab.iso8583.IsoType
import com.solab.iso8583.parse.ConfigParser
import org.hank.demoproject.BaseViewBindingActivity
import org.hank.demoproject.R
import org.hank.demoproject.databinding.ActivityISO8583TestBinding


class ISO8583TestActivity : BaseViewBindingActivity<ActivityISO8583TestBinding>() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_i_s_o8583_test)

        viewBinding.btnPack.setOnClickListener {
            var message = buildMessage()
            System.out.println(String(message.writeData()))
            viewBinding.textContent.text = Gson().toJson(message)
        }

        viewBinding.btnUnpack.setOnClickListener {
            var msgReal =
                "ISO0260000700200B238C48128A1801A0000000000000018000000000000005000121115590444438215585212111211554105100110000000010035549532******7814=******************034615444382HP640285        Brodske - Breclav (D2)Kuty         SK SK027642100             000000009780160100DVL1+0000000019        000        464& 0000900464! 0400020                   Y ! B200158 7FF9000080000000800054F85296EE7BA68300000000500000000000000039000221703978201211000AC71C9900180111A74003040000000000000000000000FF0000000000000000000000000000! B300080 4F0000000000E0F8C8000000000000002200024403020007A0000000041010000000000000000000! B400020 05151001440302    0 ! C000026         908 85            ! C400012 000000000082! P500016 \\RI3200518714\\00! CH00040                                      1  009382000000012          1 "
            var factory = ConfigParser.createDefault()
            var unpackMessage = factory.parseMessage(msgReal.toByteArray(), 12)
            viewBinding.textContent.text = Gson().toJson(unpackMessage)
            System.out.println("unpackMsg" + String(unpackMessage.writeData()))
        }
        supportActionBar!!.title = "ISO8583"
    }

    override fun inflateLayout(layoutInflater: LayoutInflater): ActivityISO8583TestBinding {
        return ActivityISO8583TestBinding.inflate(layoutInflater)
    }

    fun buildMessage(): IsoMessage {
        var message = IsoMessage()
        message.type = 0x0200
        message.setValue(3, "000000", IsoType.NUMERIC, 6)
        message.setValue(4, "50.33", IsoType.AMOUNT, 12)
        message.setValue(7, "1211155904", IsoType.DATE10, 10)
        message.setValue(11, "444382", IsoType.NUMERIC, 6)
        message.setValue(12, "155852", IsoType.TIME, 6)
        message.setValue(13, "1211", IsoType.DATE4, 4)
        message.setValue(17, "1211", IsoType.DATE4, 4)
        message.setValue(18, "5541", IsoType.NUMERIC, 4)
        message.setValue(22, "0510", IsoType.NUMERIC, 4)
        message.setValue(25, "01", IsoType.NUMERIC, 2)
        message.setValue(32, "0000000100", IsoType.LLVAR, 10)
        message.setValue(35, "549532******7814=******************", IsoType.LLVAR, 37)
        message.setValue(37, "034615444382", IsoType.NUMERIC, 12)
        message.setValue(41, "HP640285        ", IsoType.ALPHA, 16)
        message.setValue(43, "Brodske - Breclav (D2)Kuty         SK SK", IsoType.ALPHA, 40)
        message.setValue(48, "642100             00000000", IsoType.LLLVAR, 30)
        message.setValue(49, "978", IsoType.NUMERIC, 3)
        message.setValue(60, "0100DVL1+0000000", IsoType.LLLVAR, 19)
        message.setValue(61, "        000        ", IsoType.LLLVAR, 22)
        message.setValue(
            63,
            "& 0000900464! 0400020                   Y ! B200158 7FF9000080000000800054F85296EE7BA68300000000500000000000000039000221703978201211000AC71C9900180111A74003040000000000000000000000FF0000000000000000000000000000! B300080 4F0000000000E0F8C8000000000000002200024403020007A0000000041010000000000000000000! B400020 05151001440302    0 ! C000026         908 85            ! C400012 000000000082! P500016 \\RI3200518714\\00! CH00040                                      1  ",
            IsoType.LLLVAR,
            999
        )
        message.setValue(124, "009382000000", IsoType.NUMERIC, 12)
        message.setValue(125, "012          1 ", IsoType.NUMERIC, 15)
        message.isoHeader = "ISO026000070"
        return message
    }

}